*===============================================================================
* Project title		Savings Accounts to Borrow Less	
* PIs			Felipe Kast, Dina Pomeranz 
* File name		2d_tables_analysis_subwellbe.do
*-------------------------------------------------------------------------------
* Description		Creates subjective well-being tables 
* Outputs		Tables 6, A18
* Spaces per tab 	8 
*===============================================================================

*===============================================================================
*TOC
*1)	Prepare data for analysis
*2)	Tables
*2.1)	Subject well being table
*2.2)	Effect of job loss & business downturn on anxiety about financial future
*	and recent economic difficulty
*===============================================================================

*===============================================================================
* Section 1 - Prepare data for analysis
*===============================================================================

clear all
global path "~/file_server/project_savings/2_shared/impact/do/November2019"	// Path to processed data
global results "$path/results"		
cd "$results"									// Change to directory to store results
set more off

*Loading processed data
	use $path/datasets/impactDatabase.dta, clear

*===============================================================================
* Section 2 - Tables 
*===============================================================================
* Section 2.1 - Table on Subject well-being 
*-------------------------------------------------------------------------------
* 2.1.1.Calculates table content ---------------------------------------------
*-------------------------------------------------------------------------------


preserve
	eststo clear
	local i = 1	

	foreach y in anxietyFuture economicDifficulty { 
		reg d.`y' // Mark subjects nonmissing in both periods
		egen t1 = max(e(sample)), by(idBaseSurvey)		
		reghdfe `y' c.accountAccess#c.post post if t1 == 1,  		/// Regressing well-being outcomes on treatment x post 
		abs(idBaseSurvey) vce(cluster groupId) 				// Individual FE, SEs clustered at group level

		
	//Adding control mean		
		sum `y' if accountAccess == 0 & post == 1
		if (abs(`r(mean)') < 100) {
			estadd local control_mean = trim("`: display %12.3fc r(mean)'")
		} 
		else {
			estadd local control_mean = trim("`: display %12.0fc r(mean)'")
		}
		
	//AES calculation, step 1: normalizes variables
		sum `y' if accountAccess==0 //computes sd
		gen aes_cc`i' = `y'/ r(sd) //normalizes outcome variable
		
		distinct idBaseSurvey  if e(sample) 				// Adding N and FE to estimates stored 
		estadd scalar num_ind = r(ndistinct)
		estadd local ind_fixeff = "Yes"

		drop t1
		
		eststo
		local i = `i' + 1 //updates counter	
		
	}
	
//AES calculation, step 2: reshapes the data to implement regression
// we keep only variables required
	keep aes_cc* idBaseSurvey post economicShock2009 groupId accountAccess asesor2008
	reshape long aes_cc, i(idBaseSurvey post) j(out)			// "out" is the 2 well-being measures 
	
//AES: Implements regression
	reghdfe aes_cc (c.accountAccess#c.post)#i.out 				/// Regressing normalised consumption variables on treatment x post x out to use in AES calculation below
	c.accountAccess#i.out c.post#i.out i.out,				///
	abs(idBaseSurvey) vce(cluster groupId) 

//AES: computes AES								// Linear combination of estimates from above regression 
	lincom( (1.out#c.accountAccess#c.post + 2.out#c.accountAccess#c.post) / 2)	

	local aes_se = trim("`: display %12.3fc r(se)'")
	local aes_p = 2*ttail(`r(df)',abs(`r(estimate)' / `r(se)'))

// stores stars for coefficient
	if (`aes_p' <= 0.1 & `aes_p' > 0.05 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'*")
	}
	else if (`aes_p' <= 0.05 & `aes_p' > 0.01 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'**")
	}
	else if (`aes_p' <= 0.01 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'***")
	}
	else {
		local aes_b = trim("`: display %12.3fc r(estimate)'")
	}

*-------------------------------------------------------------------------------
* 2.1.2.Writes table -----------------------------------------------------------
*-------------------------------------------------------------------------------

	esttab est* using tab_subwellbe.tex, replace nonumber 						///
	cells(b(star fmt(%12.3fc)) se(par fmt(%12.3fc))) label style(tex) nonumber			///
	stats(control_mean num_ind N ind_fixeff, fmt(%12.3fc %12.0fc %12.0fc)				///
	label("Control mean" "\hline Individuals" "Observations"					///
	"Individual FE")) mlabels(,none) 								///	
	collabels(, none) eqlabels(, none)  								///
	keep(c.accountAccess#c.post )									///
	varlabels(c.accountAccess#c.post "Account $\times$ post") 					///
	starlevels(* 0.1 ** 0.05 *** 0.01) 								///
	prehead(\begin{tabular}{l*{@M}{c}} \hline \hline 						///	
	&(1) & (2) \\ 											///	
	&Anxiety about & Recent economic \\ 								///
	& financial future & difficulty \\)  								///
	posthead(\hline ) prefoot( ) 									///
	postfoot(\hline \multicolumn{@span}{c}{\textbf{AES:} `aes_b' (`aes_se')} \\ 			///
	\hline \hline \end{tabular})
 	
restore	

*===============================================================================
* Section 2.2 - Table on effect of jobloss and economic downturn on subjective WB
*===============================================================================
* 2.2.1.Calculates table content -----------------------------------------------
*-------------------------------------------------------------------------------
	eststo clear
	local i = 1
//job loss regressions
	foreach y in anxietyFuture economicDifficulty {	
		
		reg d.`y'
		egen t1 = max(e(sample)), by(idBaseSurvey)								
				
		reghdfe `y' c.jobLoss#c.post if t1 == 1,			/// Regressing on job loss x post 
		abs(i.idBaseSurvey) vce(cluster groupId) 
	 
	 
	 //adding control mean		
		sum `y' if accountAccess == 0 & post == 1
		if (abs(`r(mean)') < 100) {
			estadd local control_mean = trim("`: display %12.3fc r(mean)'")
		} 
		else {
			estadd local control_mean = trim("`: display %12.0fc r(mean)'")
		}
			
		drop t1
		distinct idBaseSurvey  if e(sample)
		estadd scalar num_ind = r(ndistinct)
		estadd local ind_fixeff = "Yes"
		 
		eststo A`i'
		local i = `i' + 1
	}
	
	
//economic downturn regressions	
	foreach y in anxietyFuture economicDifficulty {
		reg d.`y'
		egen t1 = max(e(sample)), by(idBaseSurvey)
		
		reghdfe `y' c.bizDown#c.post if t1 == 1, 			/// Regressing on economic downturn x post 
		abs(i.idBaseSurvey) vce(cluster groupId)		
		 
	 //adding control mean		
		sum `y' if accountAccess == 0 & post == 1
		if (abs(`r(mean)') < 100) {
			estadd local control_mean = trim("`: display %12.3fc r(mean)'")
		} 
		else {
			estadd local control_mean = trim("`: display %12.0fc r(mean)'")
		}
			
		drop t1
		distinct idBaseSurvey  if e(sample)
		estadd scalar num_ind = r(ndistinct)
		estadd local ind_fixeff = "Yes"
		 
		eststo B`i'
		local i = `i' + 1
}
*-------------------------------------------------------------------------------	
* 2.2.2. Writes table ---------------------------------------------------------
*-------------------------------------------------------------------------------
		
	esttab A* using tab_jobloss_bizdown.tex, replace 				///
	cells(b(star fmt(%12.3fc)) se(par fmt(%12.3fc))) label style(tex) nonumber	///
	stats(control_mean, fmt(%12.3fc)						///
	label("Control mean" "" )) mlabels(,none) 					///	
	collabels(, none) eqlabels(, none)  						///
	keep(c.jobLoss#c.post)								///
	varlabels(c.jobLoss#c.post "Job loss $\times$ post") 				///
	starlevels(* 0.1 ** 0.05 *** 0.01) 						///
	prehead(\begin{tabular}{l*{@M}{c}} \hline \hline \ 				///
	& (1) & (2) \\ 									///
	& Future anxiety & Economic difficulty \\ ) 					///
	posthead(\hline \\ \multicolumn{@span}{l}{\textbf{Panel A: Job Loss } } \\ ) 	///
	prefoot( ) postfoot( )
					
	esttab B* using tab_jobloss_bizdown.tex, append 									///
	cells(b(star fmt(%12.3fc)) se(par fmt(%12.3fc))) label style(tex) nonumber						///
	stats(control_mean num_ind N ind_fixeff, fmt(%12.0fc %12.0fc %12.0fc) 							///
	label("Control mean" "\\ \hline Individuals" "Observations" " Individual FE")) mlabels(,none)				///
	collabels(, none) eqlabels(, none)  											///
	keep(c.bizDown#c.post) varlabels(c.bizDown#c.post "Business downturn $\times$ post")					///
	starlevels(* 0.1 ** 0.05 *** 0.01) 											///	
	prehead( ) 														///
	posthead(\\ \multicolumn{@span}{l}{\textbf{Panel B: Business Downturn} } \\)  						///
	prefoot( ) postfoot(\hline \hline \end{tabular})

